package com.ruoyi.project.storage.metadata.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.storage.metadata.domain.Metadata;
import com.ruoyi.project.storage.metadata.service.IMetadataService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 数据定义Controller
 * 
 * @author shixiangwen
 * @date 2020-02-27
 */
@Controller
@RequestMapping("/storage/metadata")
public class MetadataController extends BaseController
{
    private String prefix = "storage/metadata";

    @Autowired
    private IMetadataService metadataService;

    @RequiresPermissions("storage:metadata:view")
    @GetMapping()
    public String metadata()
    {
        return prefix + "/metadata";
    }

    /**
     * 查询数据定义列表
     */
    @RequiresPermissions("storage:metadata:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Metadata metadata)
    {
        startPage();
        List<Metadata> list = metadataService.selectMetadataList(metadata);
        return getDataTable(list);
    }

    /**
     * 导出数据定义列表
     */
    @RequiresPermissions("storage:metadata:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Metadata metadata)
    {
        List<Metadata> list = metadataService.selectMetadataList(metadata);
        ExcelUtil<Metadata> util = new ExcelUtil<Metadata>(Metadata.class);
        return util.exportExcel(list, "metadata");
    }

    /**
     * 新增数据定义
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据定义
     */
    @RequiresPermissions("storage:metadata:add")
    @Log(title = "数据定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Metadata metadata)
    {
        return toAjax(metadataService.insertMetadata(metadata));
    }

    /**
     * 修改数据定义
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Metadata metadata = metadataService.selectMetadataById(id);
        mmap.put("metadata", metadata);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据定义
     */
    @RequiresPermissions("storage:metadata:edit")
    @Log(title = "数据定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Metadata metadata)
    {
        return toAjax(metadataService.updateMetadata(metadata));
    }

    /**
     * 删除数据定义
     */
    @RequiresPermissions("storage:metadata:remove")
    @Log(title = "数据定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(metadataService.deleteMetadataByIds(ids));
    }
}
